﻿Ext.define("PSI.Permission.SelectPermissionForm", {
    // idList是数组
    show: function (idList, parentForm) {
        this.parentForm = parentForm;
        if (!Ext.ModelManager.isRegistered("PSIPermission")) {
            Ext.define("PSIPermission", {
                extend: "Ext.data.Model",
                fields: ["id", "name"]
            });
        }

        var permissionStore = Ext.create("Ext.data.Store", {
            model: "PSIPermission",
            autoLoad: false,
            data: []
        });

        var permissionGrid = Ext.create("Ext.grid.Panel", {
            title: "角色的权限",
            padding: 5,
            selModel: {
                mode: "MULTI"
            },
            selType: "checkboxmodel",
            store: permissionStore,
            columns: [
                { header: "权限名称", dataIndex: "name", flex: 1, menuDisabled: true }
            ]
        });

        this.permissionGrid = permissionGrid;

        var wnd = Ext.create("Ext.window.Window", {
            title: "选择权限",
            width: 400,
            height: 300,
            modal: true,
            layout: "fit",
            items: [permissionGrid],
            buttons: [
                {
                    text: "确定",
                    formBind: true,
                    iconCls: "psi-button-ok",
                    handler: this.onOK,
                    scope: this
                },
                { text: "取消", handler: function () { this.wnd.close(); }, scope: this }
            ]
        });

        this.wnd = wnd;

        wnd.show();

        var el = wnd.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/Permission/SelectPermission",
            params: { idList: idList.join() },
            method: "POST",
            callback: function (options, success, response) {
                permissionStore.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);

                    for (var i = 0; i < data.length; i++) {
                        var item = data[i];
                        permissionStore.add({ id: item.id, name: item.name });
                    }
                }

                el.unmask();
            }
        });
    },

    onOK: function () {
        var grid = this.permissionGrid;

        var items = grid.getSelectionModel().getSelection();
        if (items == null || items.length == 0) {
            PSI.MsgBox.showInfo("没有选择权限");

            return;
        }

        if (this.parentForm) {
            this.parentForm.setSelectedPermission(items);
        }

        this.wnd.close();
    }
});